#include "UIProgressBar.h"

UIProgressBar::UIProgressBar()
{
	Position = { 40, 60 };
	Width = 220;
	Height = 20;
}

UIProgressBar::UIProgressBar(float xPos, float yPos) : UIElement(xPos, yPos)
{
	Width = 220;
	Height = 20;
}

UIProgressBar::UIProgressBar(float xPos, float yPos, float width, float height) : UIElement(xPos, yPos, width, height)
{
}

float __stdcall UIProgressBar::calculateProgressBarWidth(float value)
{
	float percentage = value / MaxValue;
	return Width * percentage;
}

void __stdcall UIProgressBar::Draw(Graphics* graphics)
{
	if (Value > MaxValue) Value = MaxValue;
	if (Value < MinValue) Value = MinValue;
	float CurrentValue = Value;
	CurrentValue = roundf(CurrentValue / Interval) * Interval;

	if (Value != m_PreviousValue)
		for (progress_callback_function cb : m_ProgressCallbacks)
			cb(this);

	m_PreviousValue = GetRoundedValue();

	if (!this->RoundedCorners)
	{
		// Drawing the area for the progress bar
		graphics->DrawRectangle(Position.x, Position.y, Width, Height, color.r, color.g, color.b, color.a, 1.0f, true);

		// Drawing the progress bar
		graphics->DrawRectangle(Position.x, Position.y, calculateProgressBarWidth(CurrentValue), Height, ProgressColor.r, ProgressColor.g, ProgressColor.b, ProgressColor.a, 1.0f, true);
	}
	else
	{
		// Drawing the area for the progress bar
		graphics->DrawRoundedRectangle(Position.x, Position.y, Width, Height, RoundCornerRadiusX, RoundCornerRadiusY, color.r, color.g, color.b, color.a, 1.0f, true);

		// Drawing the progress bar
		graphics->DrawRoundedRectangle(Position.x, Position.y, calculateProgressBarWidth(CurrentValue), Height, RoundCornerRadiusX, RoundCornerRadiusY, ProgressColor.r, ProgressColor.g, ProgressColor.b, ProgressColor.a, 1.0f, true);
	}
}
